package com.kx.yooli.kx.wechat.config.swagger;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.HeaderParameter;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author john_sqwen
 * @date 2024/3/19
 * @PROJECT_NAME wechat
 * @description create Knife4jConfiguration by john_sqwen
 * and swagger value is
 */
@Configuration
public class Knife4jConfiguration {
    private final Logger log = LoggerFactory.getLogger(Knife4jConfiguration.class);
    @Bean
    public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
        return openApi -> {
            if (openApi.getTags()!=null){
                openApi.getTags().forEach(tag -> {
                    Map<String,Object> map=new HashMap<>();
                    map.put("x-order", new Random().nextInt(0,100));
                    tag.setExtensions(map);
                });
            }
            if(openApi.getPaths()!=null){
                openApi.addExtension("wechat-xx","001");
                openApi.getPaths().addExtension("x-wechat",new Random().nextInt(1,100));
            }

        };
    }
    @Bean
    public GroupedOpenApi userApi(){
    String[] paths = { "/**" };
    String[] packagedToMatch = { "com.kx.yooli.kx.wechat.controller" };
    return GroupedOpenApi.builder().group("接口api")
            .pathsToMatch(paths)
            .addOperationCustomizer((operation, handlerMethod) -> {
                return operation.addParametersItem(new HeaderParameter().name("groupCode").example("微信").description("yooli 集团").schema(new StringSchema()._default("BR").name("groupCode").description("yooli 集团")));
            })
            .packagesToScan(packagedToMatch).build();
    }
    @Bean
    public OpenAPI customOpenAPI() {
    return new OpenAPI()
        .info(new Info()
                .title("生产系统API")
                .version("1.0")
                .description( "Knife4j集成springdoc-openapi示例")
                .termsOfService("https://open.weixin.com/")
                .license(new License().name("Apache 2.0")
                        .url("https://weixin.yooli.com"))
        ).addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION))
        .components(new Components().addSecuritySchemes(HttpHeaders.AUTHORIZATION,new SecurityScheme()
                .name(HttpHeaders.AUTHORIZATION).type(SecurityScheme.Type.HTTP).scheme("open.wechat")));
    }


}
